<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



include "/library/stock.php";


if(isset($_GET['searchID'])){
    query_product($_GET['searchID']);
    exit();
}
if(isset($_GET['ratio'])&&isset($_GET['id'])&&isset($_GET['sort'])&&isset($_GET['addStock'])){
    addStock($_GET);
    
    //header('location: customer_type.php');
}
if(isset($_GET['edit'])){
    editProduct($_GET);
    //header('location: product.php');
}
include "header.php";
?>
<head>
<script type="text/javascript" src="js/product_add.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#Select').hide();
        $('#product_name').parent('').hide();
        $('#product_type').parent('').hide();
        $('#ratio').parent('').hide();
        $('#buyprice').parent('').hide();
        $('#addStock').parent('').hide();
        $('#unitname').parent('').hide();

        $('#product_id :button').click(function(){
            var globaldata = null;
            $.getJSON("stock_add.php",{'searchID':"'"+$('#product_id :text').val()+"'"}, function(data){
               globaldata=data;
                $('#product_name').html(data.product.name+"<input type='hidden' name='id' value='"+data.product.id+"' >");
                var tblRow ='<option>เลือกประเภทการขาย</option>';
                $.each(data.type, function(i,result){
					tblRow = tblRow + "<option value="+result.id+">"+result.name+"</option>";
					
		});
                $('#product_name').parent('').show();
                $('#product_type').parent('').show();
                $('#Select').show();
                $("#Select").html(tblRow);
            });
            $("#Select").change(function(){
                //$('#unitname').html('dsa123d');
                $.each(globaldata.type, function(i,result){
                    if($('#Select').val() == result.id){
                        $('#ratio').parent('').show();
                        $('#buyprice').parent('').show();
                        $('#addStock').parent('').show();
                        $('#unitname').parent('').show();
                        $('#unitname').html(result.nameunit);
                        $('#ratio').html(result.ratio+'<input type="hidden" name="ratio" value="'+result.ratio+'">');
                        $('#buyprice').html(result.buyprice);

                    }
		});
            });
        });
        $('#form').submit(function (){
            if($('#addStock :text').val()!="" && $('#product_id :text').val()!="" && $('#Select option:selected').length!=0)
                $('#form').submit();
            else
                return false;
        });
    });


</script>
</head>
<div id="product" align="center" width="100%">
    <form action="stock_add.php" method="get" id="form">
    <table  id="tableLine" >
        <tr>
            <th>รหัสสินค้า</th>
            <td id="product_id" ><input type="text"  name="product_id" value=""><input type="button" value="ค้นหา"></td>
        </tr>
        <tr>
            <th >ชื่อสินค้า</th>
            <td id="product_name"></td>
        </tr>
        <tr>
            <th >ประเภทการขาย</th>
            <td id="product_type"> <select name="sort" id="Select"></select></td>
        </tr>
        <tr>
            <th >ชื่อหน่วย</th>
            <td id="unitname"></td>
        </tr>
        <tr>
            <th >อัตราต่อหน่วย</th>
            <td id="ratio"></td>
        </tr>
        <tr>
            <th >ต้นทุน</th>
            <td id="buyprice"></td>
        </tr>
        <tr>
            <th >เพิ่มสินค้าจำนวน</th>
            <td id="addStock"><input type="text" name="addStock" value="<? if(!empty($editData['name'])) echo $editData['name']; ?>"></td>
        </tr>
        


    </table>
     <div >      
        <input type="submit" id="add" name="add" value=" เพิ่ม ">
</div>
    </form>

    <p>
    </p>

</div>
